package com.gaogzhen.threadLocal;

import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 * @date 2022-12-04 13:13
 */
public class MyDemo {
    private String content;
    ThreadLocal<String> tl = new ThreadLocal<>();

    public String getContent() {
//        return content;
        return tl.get();
    }

    public void setContent(String content) {
//        this.content = content;
        tl.set(content);
    }

    public void clear() {
        tl.remove();
    }

    public static void main(String[] args) throws InterruptedException {
        MyDemo myDemo = new MyDemo();
        for (int i = 0; i < 5; i++) {
            Thread thread = new Thread(() -> {
                myDemo.setContent(Thread.currentThread().getName() + "的数据");
                System.out.println("---------------");
                System.out.println(Thread.currentThread().getName() + "----->" + myDemo.getContent());
            });
            thread.setName("线程" + i);
            thread.start();
//            thread.join();
        }
//        System.out.println(myDemo.getContent());
        myDemo.clear();
    }
}
